/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.libswrapper;

import com.microsoft.tfs.core.TFSConfigurationServer;
import com.microsoft.tfs.core.TFSConnection;
import com.microsoft.tfs.core.httpclient.Credentials;
import com.microsoft.tfs.core.httpclient.UsernamePasswordCredentials;
import java.net.URI;
import java.net.URISyntaxException;
import org.openide.modules.ModuleInstall;
import org.openide.util.Exceptions;

public class Installer extends ModuleInstall {

    public static final String TFS_NATIVE_LIBS_HOME;
    
    // file:///home/venkat/NetBeansProjects/TFS-on-NB/downloads/TFS-SDK-11.0.0/readme.html#clientObjectModel
    public static final String NATIVE_LIBS_SYSTEM_PROPERTY = "com.microsoft.tfs.jni.native.base-directory";
    
    static {
        String netbeansHome = System.getProperty("netbeans.home");
        int lastSlash = netbeansHome.lastIndexOf("/");
        netbeansHome = netbeansHome.substring(0, lastSlash + 1);
        TFS_NATIVE_LIBS_HOME = netbeansHome + "tfsonnb/modules/lib/11.0.0/native";        
    }

    public Installer() {
        
    }
    
    
    
    @Override
    public void restored() {
        // Refer to file:///home/venkat/NetBeansProjects/TFS-on-NB/downloads/TFS-SDK-11.0.0/readme.html#clientObjectModel for more info.        
        System.setProperty(NATIVE_LIBS_SYSTEM_PROPERTY, TFS_NATIVE_LIBS_HOME);
        System.out.println("==================================================");
        System.out.println(NATIVE_LIBS_SYSTEM_PROPERTY);
        System.out.println(System.getProperty(NATIVE_LIBS_SYSTEM_PROPERTY));
        
        //TODO: Try to create a TFS connection here
        // Refer to file:///home/venkat/NetBeansProjects/TFS-on-NB/downloads/TFS-SDK-11.0.0/docs/javadoc/index.html com.microsoft.tfs.core.TFSConnection
        Credentials credentials = new UsernamePasswordCredentials("", "");
        
        TFSConnection connection = null;
        
        try {
            //        TFSConnection connection = new TFSConnection("https://shaggyinjun.visualstudio.com/DefaultCollection", credentials, new DefaultConnectionAdvisor(Locale.US, TimeZone.getDefault()), "");
            connection = new TFSConfigurationServer(new URI("https://shaggyinjun.visualstudio.com/DefaultCollection"), credentials);
//            connection.authenticate();
            
            System.out.println(connection);
            System.out.println("Successfully connected to TFS with credentials: " + credentials);
            System.out.println("==================================================");
        } catch (URISyntaxException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

}
